
package com.hlkj.pay.app.order.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import org.springframework.stereotype.Service;

import com.hlkj.pay.common.constants.MqConstant;
import com.hlkj.pay.config.ExtRocketMQTemplate;
import com.hlkj.pay.dto.common.OrderStatusDto;
import com.hlkj.pay.dto.merchant.MerchantAccessDto;
import com.hlkj.pay.dto.order.OrderFundDto;
import com.hlkj.pay.enums.PayOrderEnums;
import com.hlkj.pay.infrastructure.model.order.*;
import com.hlkj.pay.infrastructure.model.payment.MerchantPaymentDO;
import com.hlkj.pay.infrastructure.model.payment.MerchantPaymentDetailDO;
import com.hlkj.pay.service.merchant.IMerchantAccessService;
import com.hlkj.pay.service.order.*;
import com.hlkj.pay.service.payment.IMerchantPaymentService;
import com.hlkj.pay.task.AdPayOrderScheduledTasks;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/10 15:30
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderNotifyHandler {

    private final ExtRocketMQTemplate extRocketMQTemplate;

    private final IPayOrderService payOrderService;

    private final IRefundOrderService refundOrderService;

    private final IMerchantAccessService merchantAccessService;

    private final ISettleOrderService settleOrderService;

    private final IMerchantPaymentService merchantPaymentService;

    private final IPayOrderFundService payOrderFundService;

    private final ITransferOrderService transferOrderService;

    private final IWithdrawOrderService withdrawOrderService;

    private final OrderNotifyProcessHandler orderNotifyProcessHandler;

    private final AdPayOrderScheduledTasks adPayOrderScheduledTasks;

    /**
     *
     * @param refundOrderNo
     */
    public void orderNotify(String refundOrderNo, Integer noticeNum, PayOrderEnums.ORDER_STATUS_TYPE orderStatusType) {
        OrderStatusDto orderStatusDto = new OrderStatusDto();
        orderStatusDto.setOrderNo(refundOrderNo);
        orderStatusDto.setOrderStatusType(orderStatusType.getCode());
        try {
            if (noticeNum == null) {
                adPayOrderScheduledTasks.processOrderNotifyMessageLast(orderStatusDto);
            }
            else {
                if (noticeNum < 10) {
                    orderStatusDto.setTotalConsumerCount(noticeNum);
                    orderStatusDto.setDelaySendTime(System.currentTimeMillis());
                    log.info("非实时回调,订单号 orderNo:{},noticeNum:{}", refundOrderNo, noticeNum);
                    extRocketMQTemplate.sendDelay(noticeNum, MqConstant.MERCHANT_ORDER_NOTIFY_TOPIC, orderStatusDto);
                }
                else {
                    log.info("回调通知次数大于10次数,停止回调 refundOrderNo:{}", refundOrderNo);
                }
            }
        }
        catch (Exception e) {
            log.error("mq 发送异常:{}", e.getMessage(), e);
        }
    }



    /**
     *
     * @param orderNotifyDO
     */
    public void retryNotify(OrderNotifyDO orderNotifyDO) {
        PayOrderEnums.ORDER_STATUS_TYPE orderStatusType = PayOrderEnums.ORDER_STATUS_TYPE.from(orderNotifyDO.getNotifyType());
        String orderNo = orderNotifyDO.getPayOrderNo();
        switch (orderStatusType) {
            case PAY_ORDER_STATUS:
                PayOrderDO payOrderDO = payOrderService.queryPayOrder(orderNo);
                orderNotifyProcessHandler.notifyPayOrder(null, payOrderDO, orderNotifyDO);
                break;
            case REFUND_STATUS:
                RefundOrderDO refundOrderDO = refundOrderService.queryRefundOrder(orderNo);
                orderNotifyProcessHandler.notifyRefundOrder(null, refundOrderDO, orderNotifyDO);
                break;
            case PAYMENT_ORDER_STATUS:
                MerchantPaymentDetailDO merchantPaymentDetailDO = merchantPaymentService.queryPaymentDetail(orderNo);
                MerchantPaymentDO merchantPaymentDO = merchantPaymentService.queryDetail(merchantPaymentDetailDO.getPaymentId());
                orderNotifyProcessHandler.notifyPaymentOrder(null, merchantPaymentDO, merchantPaymentDetailDO, orderNotifyDO);
                break;
            case MERCHANT_ACCESS_STATUS:
                MerchantAccessDto merchantAccessDto = merchantAccessService.domainDetailByRequestId(orderNo);
                orderNotifyProcessHandler.notifyMerchantAccess(null, merchantAccessDto, orderNotifyDO);
                break;
            case MERCHANT_ACCESS_SETTLE_STATUS:
                OrderSettleDO orderSettleDO = settleOrderService.queryByPayOrderNo(orderNo);
                orderNotifyProcessHandler.notifyMerchantAccessSettle(null, orderSettleDO, orderNotifyDO);
                break;
            case FUND_ORDER_STATUS:
                OrderFundDto orderFundDto = payOrderFundService.queryAcctOrderFundDto(orderNo);
                orderNotifyProcessHandler.notifyOrderFund(null, orderFundDto, orderNotifyDO);
                break;
            case TRANSFER_ORDER_STATUS:
                OrderTransferDO orderTransferDO = transferOrderService.queryPayOrder(orderNo);
                orderNotifyProcessHandler.notifyOrderTransfer(null, orderTransferDO, orderNotifyDO);
                break;
            case WITHDRAW_ORDER_STATUS:
                OrderWithdrawDO orderWithdrawDO = withdrawOrderService.queryPayOrder(orderNo);
                orderNotifyProcessHandler.notifyOrderWithdraw(null, orderWithdrawDO, orderNotifyDO);
                break;
        }
    }

}
